package com.xyh.transaction.controller;

import com.github.pagehelper.PageInfo;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.xyh.transaction.common.OrderStatus;
import com.xyh.transaction.entity.dto.goods.EditOrAddGoodsDto;
import com.xyh.transaction.entity.dto.goods.GoodsDto;
import com.xyh.transaction.entity.dto.order.SearchOrderDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.entity.vo.GoodsConditionVo;
import com.xyh.transaction.entity.vo.GoodsDetailVo;
import com.xyh.transaction.entity.vo.GoodsVo;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.GoodsService;
import com.xyh.transaction.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(tags = "商品管理")
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "商品详情", notes = "根据id返回商品详情数据")
    @GetMapping()
    public ResponseResult<GoodsDetailVo> goodsDetail(@RequestParam Integer id) {
        GoodsDetailVo goodsDetailVo = goodsService.getGoodsDetail(id);
        return ResponseResult.success("查询成功！", goodsDetailVo);
    }

    @ApiOperation(value = "相关商品", notes = "根据返回当前用户相关的商品")
    @GetMapping("relevant")
    public ResponseResult<List<GoodsVo>> relevantGoods(@RequestParam Integer limit){
        List<GoodsVo> relevantGoodsList = goodsService.getRelevantGoodsList(limit);
        return ResponseResult.success("查询成功！",relevantGoodsList);
    }


    @ApiOperation(value = "热销商品", notes = "1: 24小时，2: 1周")
    @GetMapping("hot")
    public ResponseResult<List<GoodsVo>> hotList(
            @RequestParam Integer id, @RequestParam Integer type, @RequestParam Integer limit) {
        List<GoodsVo> hotList = goodsService.getHotList();
        return ResponseResult.success("查询成功!", hotList);
    }

    @ApiOperation(value = "商品列表", notes = "返回所有商品列表")
    @PostMapping("list")
    public ResponseResult<PageInfo<GoodsVo>> goodsList(@RequestBody GoodsDto goodsDto) {
        PageInfo<GoodsVo> goodsList = goodsService.getGoodsList(goodsDto);
        return ResponseResult.success("查询成功！", goodsList);
    }

    @ApiOperation(value = "删除商品", notes = "删除商品")
    @PostMapping("delete")
    public ResponseResult<Void> deleteGoods(@RequestBody ArrDto arrDto) throws MeilisearchException {
        goodsService.deleteGoods(arrDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "添加商品", notes = "添加商品")
    @PostMapping("add")
    public ResponseResult<Void> addGoods(@RequestBody EditOrAddGoodsDto editOrAddGoodsDto) throws MeilisearchException {
        goodsService.addGoods(editOrAddGoodsDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "修改商品", notes = "根据id修改商品")
    @PutMapping("edit")
    public ResponseResult<Void> editGoods(@RequestBody EditOrAddGoodsDto editOrAddGoodsDto) throws MeilisearchException {
        goodsService.editGoods(editOrAddGoodsDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "搜索商品", notes = "搜索商品信息")
    @GetMapping("search")
    public ResponseResult<List<GoodsVo>> searchGoods(String searchText) {
        List<GoodsVo> goodsVos = goodsService.searchGoods(searchText);
        return ResponseResult.success("查询成功！", goodsVos);
    }

    @ApiOperation(value = "成色列表", notes = "返回所有成色列表")
    @GetMapping("/condition")
    public ResponseResult<List<GoodsConditionVo>> conditionList() {
        List<GoodsConditionVo> conditionList = goodsService.getConditionList();
        return ResponseResult.success("查询成功！", conditionList);
    }

    @ApiOperation(value = "评价商品", notes = "保存商品的评价信息")
    @PostMapping("/evaluation")
    public ResponseResult<Void> saveEvaluationInfo(@RequestBody List<GoodsDto> goodsDtos) {
        goodsService.saveEvaluation(goodsDtos);
        // 更新订单状态
        SearchOrderDto searchOrderDto = new SearchOrderDto();
        if (!goodsDtos.isEmpty()) {
            searchOrderDto.setId(goodsDtos.get(0).getOrderId());
            searchOrderDto.setStatus(OrderStatus.COMPLETE.ordinal());
            orderService.updateStatus(searchOrderDto);
        }
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "导出商品", notes = "导出商品数据xlsx")
    @PostMapping("export")
    public ResponseEntity<byte[]> export(@RequestBody GoodsDto goodsDto) {
        return goodsService.exportGoodsList(goodsDto);
    }

    @ApiOperation(value = "导入商品", notes = "xlsx批量导入商品数据")
    @PostMapping("import")
    public ResponseResult<Void> importGoods(
            @RequestParam("file") MultipartFile file,
            @RequestParam Map<String, String> params) {
        Boolean isCover = Boolean.valueOf(params.get("isCover"));
        goodsService.importGoods(file, isCover);
        return ResponseResult.success("操作成功！",null);
    }
}
